//package com.learn.framework.shiro.realm;
//
//import java.util.Set;
//
//import org.apache.shiro.SecurityUtils;
//import org.apache.shiro.authc.AuthenticationException;
//import org.apache.shiro.authc.AuthenticationInfo;
//import org.apache.shiro.authc.AuthenticationToken;
//import org.apache.shiro.authc.SimpleAuthenticationInfo;
//import org.apache.shiro.authc.UsernamePasswordToken;
//import org.apache.shiro.authz.AuthorizationInfo;
//import org.apache.shiro.authz.SimpleAuthorizationInfo;
//import org.apache.shiro.realm.AuthorizingRealm;
//import org.apache.shiro.subject.PrincipalCollection;
//import com.learn.common.utils.security.ShiroUtils;
//import com.learn.framework.shiro.service.LoginService;
//import com.learn.project.system.menu.service.IMenuService;
//import com.learn.project.system.role.service.IRoleService;
//import com.learn.project.system.user.domain.User;
//import org.springframework.beans.factory.annotation.Autowired;
//
///**
// * 自定义Realm 处理登录 权限
// *
// * @author Crown
// */
//public class UserRealm extends AuthorizingRealm {
//
//    @Autowired
//    private IMenuService menuService;
//
//    @Autowired
//    private IRoleService roleService;
//
//    @Autowired
//    private LoginService loginService;
//
//    /**
//     * 授权
//     */
//    @Override
//    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {
//        User user = ShiroUtils.getSysUser();
//        // 角色列表
//        Set<String> roles;
//        // 功能列表
//        Set<String> menus;
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        // 管理员拥有所有权限
//        if (user.isAdmin()) {
//            info.addRole("admin");
//            info.addStringPermission("*:*:*");
//        } else {
//            roles = roleService.selectRoleKeys(user.getUserId());
//            menus = menuService.selectPermsByUserId(user.getUserId());
//            // 角色加入AuthorizationInfo认证对象
//            info.setRoles(roles);
//            // 权限加入AuthorizationInfo认证对象
//            info.setStringPermissions(menus);
//        }
//        return info;
//    }
//
//    /**
//     * 登录认证
//     */
//    @Override
//    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
//        String username = upToken.getUsername();
//        String password = "";
//        if (upToken.getPassword() != null) {
//            password = new String(upToken.getPassword());
//        }
//        User user = loginService.login(username, password);
//        return new SimpleAuthenticationInfo(user, password, getName());
//    }
//
//    /**
//     * 清理缓存权限
//     */
//    public void clearCachedAuthorizationInfo() {
//        this.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
//    }
//}
